package com.gorillalogic.scrummonkey.dms {
	import mx.collections.ArrayCollection;
	import com.adobe.fiber.core.model_internal;
	
	[RemoteClass(alias="ScrumDMS.Iteration")]
	public class Iteration extends _Super_Iteration {
		
		/** 
		 * DO NOT MODIFY THIS STATIC INITIALIZER - IT IS NECESSARY
		 * FOR PROPERLY SETTING UP THE REMOTE CLASS ALIAS FOR THIS CLASS
		 *
		 **/
		
		/**
		 * Calling this static function will initialize RemoteClass aliases
		 * for this value object as well as all of the value objects corresponding
		 * to entities associated to this value object's entity.  
		 */     
		public static function _initRemoteClassAlias() : void
		{
			_Super_Iteration.model_internal::initRemoteClassAliasSingle(com.gorillalogic.scrummonkey.dms.Iteration);
			_Super_Iteration.model_internal::initRemoteClassAliasAllRelated();
		}
		
		model_internal static function initRemoteClassAliasSingleChild() : void 
		{
			_Super_Iteration.model_internal::initRemoteClassAliasSingle(com.gorillalogic.scrummonkey.dms.Iteration);
		}
		
		{
			_Super_Iteration.model_internal::initRemoteClassAliasSingle(com.gorillalogic.scrummonkey.dms.Iteration);
		}
		/** 
		 * END OF DO NOT MODIFY SECTION
		 *
		 **/  
		
		public function Iteration(name:String=null,description:String=null) {
			super();
			this.name = name;
			this.description = description;
			this.objectives = new ArrayCollection();
		}

		public function removeObjective(objective:Objective):void {
			objective.unassign(this);
		}

		public function addObjective(objective:Objective):void {
			objective.assign(this);
		}

		/** Assign iteration to given release, and if necessary
		 *  unassign from old release.
		 */
		public function assign(release:Release):void {
			if (release != null && release != this.release) {
				if (this.release != null) {
					//unassign this iteration from current release
					var i:int = this.release.iterations.getItemIndex(this);
					this.release.iterations.removeItemAt(i);
				}

				//assign this iteration to new release
				this.release = release;
				release.iterations.addItem(this);

				//reassign any objectives too
				this.objectives.toArray().forEach(
					function (o:Objective, idx:int, objectives:Array):void {
						var i:int = o.release.objectives.getItemIndex(o);
						o.release.objectives.removeItemAt(i);

						o.release = release;
						release.objectives.addItem(o);
					});
			}
		}
	}
}